<?php
namespace app\admin\controller;
use app\admin\controller\Common;
use app\service\communityService;
use app\service\apiService;
use think\Db;
class Community extends Common{
    //查询小区信息
    public function communitylist(){
        $com = $this->pidTokenId();
        $where['id'] = array('in',$com['community_id']);
        //查询小区信息
        $list = (new communityService())->communitylist($where);
        $this->assign('list', $list);
        //查询数据条数
        $number =Db::table("community")->where($where)->count();
        $this->assign('number', $number);
        return $this->fetch('community_list');
    }
    //添加小区信息
    public function communityAdd(){
        //根据当前账号查询物业信息
        $com = $this->pidTokenId();
        //echo "<pre>";
        //var_dump($com);die;
        $where['id'] = array('in',$com['property_id']);
       
        if(request()->isPost()){
            //获取表单数据
            $data = request()->post();
            //判断小区是否重复
            $info = (new communityService())->communityName($data['name']);
            if($info){
                $this->error('项目已添加');
            }
            $data['time'] = date('Y-m-d H:i:s',time());
            //将数据插入数据库
            $result = (new communityService())->communityAdd($data);
            //判断数据是否插入成功
            if($result){
                $this->success('项目添加成功',url('communitylist'));
            }else{
                $this->error('项目添加失败',url('communityAdd'));
            }
        }else{
            return $this->fetch('community_add');
        }
    }

    //修改物业小区信息
    public function communityEdit() {
        //查询物业支付宝账号和token
        $com = $this->pidTokenId();
        $where['id'] = array('in',$com['property_id']);
        $propertylist =(new communityService())->propertyList($where);
        $this->assign('propertylist',$propertylist);
        if (request()->isGet()) {
            $id = request()->param('id');
            if(!isset($id) && empty($id)){
                $this->error('小区不存在');
            }
            $info = (new communityService())->communityIdInfo($id);
            $this->assign('info', $info);
            return $this->fetch('community_edit');
        }else{
            $data = request()->post();
			//print_r($data);
			//die;
            $result = (new communityService())->communityEdit($data);
            if($result) {
                $this->success('修改成功');
            }else{
                $this->error('修改失败');
            }
        }
    }

    //添加小区打印机
    public function printing(){
        if(request()->isGet()){
            $id = request()->param('id');
            if(!isset($id) && empty($id)){
                $this->error('小区不存在');
            }
            $info = (new communityService())->communityIdInfo($id);
            $this->assign('info',$info);
            if($info){
                return $this->fetch('community_printing');
            }else{
                $this->error('小区信息错误');
            }
        }else{
            //获取表单数据
            $data = request()->post();
            //将数据插入数据库
            $result = (new communityService())->communityEdit($data);
            //判断数据是否插入成功
            if($result){
                $this->success('配置打印机成功',url('communitylist'));
            }else{
                $this->error('配置打印机失败',url('communitylist'));
            }
        }
    }
    
    public function communityDel(){
        $id = request()->param('id');
        $validate = $this->validate(request()->param(),[
            'id'    =>  'require|number'
        ]);
        if($validate !== true){
            return json(["error"=>$validate]);
            $this->error($validate);
        }
        //判断是否存在该数据
        $info = (new communityService())->communityIdInfo($id);
        if(empty($info)){
            return json(["error"=>'该小区不存在']);
            $this->error('该小区不存在');
        }
        //
        //查询该小区下面是否存在房屋
        $roominfo=Db::table("room")->where("community_id",$id)->find();
        if(!empty($roominfo)){
            return json(["error"=>"请删除该小区下面的房屋"]);
            $this->error('请删除该物业下面的小区');
        }
        $result = Db::name('community')->delete($id);
        if($result){
            return json(["info"=>'删除成功']);
            $this->success('删除成功',url('communityList'));
        }else{
            return json(["error"=>'删除失败']);
            $this->success('删除失败',url('communityList'));
        }
    }

    //查询小区信息
    public function gatelist(){
        $com = $this->pidTokenId();
        $where['id'] = array('in',$com['community_id']);
        //查询小区信息
        $list = (new communityService())->gateList($where);
        $this->assign('list', $list);
        //查询数据条数
        $number =Db::table("community_gate")->where($where)->count();
        $this->assign('number', $number);
        return $this->fetch('gate_list');
    }

    public function gateAdd(){
        if(request()->isPost()){
            $data=request()->post();


            $communityinfo=Db::table("community")->where("id",$data["community_id"])->find();
            $data['community_name'] = $communityinfo["name"];

            //添加时间
            $data['create_time'] =time();
            $data['update_time'] =time();
            //数据插入到数据库
            $result = Db::table("community_gate")->insert($data);
            if($result){
                $this->success('添加成功',url('gatelist'));
            }else{
                $this->error('添加失败',url('gatelist'));
            }
        }else{
            //查询小区信息
            $com = $this->pidTokenId();

            $whe['id'] = array('in',$com['community_id']);
            $communitylist = Db::name('community')->where($whe)->select();
            $community_id = $com['community_id'];
            $this->assign('communitylist',$communitylist);
            return $this->fetch("gate_add");
        }
    }


    public function contractlist(){
        $com = $this->pidTokenId();
        $where['community_id'] = array('in',$com['community_id']);
        //查询小区信息
        $list = Db::name('community_contract')->where($where)->order('id asc')->select();
        //var_dump($list);
        $this->assign('list', $list);
        //查询数据条数
        $number =Db::table("community_contract")->where($where)->count();
        $this->assign('number', $number);
        return $this->fetch('contract_list');
    }
    public function contractAdd()
    {
        if (request()->isPost()) {
            $data = request()->post();


            $communityinfo = Db::table("community")->where("id", $data["community_id"])->find();
            $data['community_name'] = $communityinfo["name"];

            //添加时间
            $data['create_time'] = time();
            $data['update_time'] = time();
            //数据插入到数据库
            $result = Db::table("community_contract")->insert($data);
            if ($result) {
                $this->success('添加成功', url('gatelist'));
            } else {
                $this->error('添加失败', url('gatelist'));
            }
        } else {
            //查询小区信息
            $com = $this->pidTokenId();

            $whe['id'] = array('in', $com['community_id']);
            $communitylist = Db::name('community')->where($whe)->select();
            $community_id = $com['community_id'];
            $this->assign('communitylist', $communitylist);
            return $this->fetch("contract_add");
        }
    }
    public function adminlist(){
        $community_id=request()->get("community_id");

        $userlist=Db::table("community_admin")
            ->select();
        //echo "<pre>";
        //dump($userlist);die;
        $this->assign("userlist",$userlist);
        return $this->fetch("community_adminlist");
    }
}